package com.factory.iamp.dao;

import com.factory.iamp.pojo.Line;
import com.factory.iamp.pojo.LinePermission;
import com.factory.iamp.pojo.LineUserList;
import com.factory.iamp.pojo.LineWithInfo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface LineMapper {
    @Select({
            "select * from line inner join Line_Permission ",
            "on line.lid = Line_Permission.lid where Line_Permission.uid = #{uid} and Line_Permission.r = 1",
            "order by line.lid asc"
    })
    @Results({
            @Result(property = "lid", column = "lid"),
            @Result(property = "userList", column = "lid", one = @One(select = "com.factory.iamp.dao.LineMapper.getMemberInLineAll")),
    })
    List<LineWithInfo> getAllLine(@Param("uid") String uid);

    @Select("select * from `Line_Permission` where `lid` = #{lid} and `uid` = #{uid}")
    LinePermission getPermission(@Param("lid") int lid, @Param("uid") String uid);

    @Select("select count(*) from `Line_Permission` where `lid` = #{lid}")
    int getMemberCountInLine(@Param("lid") int lid);

    @Select("select * from `Line_Permission` where `lid` = #{lid}")
    @Results({
            @Result(property = "userinfo", column = "uid", one = @One(select = "com.factory.iamp.dao.UserMapper.findByIdUserName"))
    })
    List<LineUserList> getMemberInLineAll(@Param("lid") int lid);

    @Select("select * from `Line_Permission` where `lid` = #{lid}")
    @Results({
            @Result(property = "userinfo", column = "uid", one = @One(select = "com.factory.iamp.dao.UserMapper.findById"))
    })
    List<LineUserList> getMemberInLineOne(@Param("lid") int lid, @Param("uid") int uid);

    @Select("select * from `Line_Permission` where `lid` = #{lid} limit #{offset}, #{limit}")
    @Results({
            @Result(property = "userinfo", column = "uid", one = @One(select = "com.factory.iamp.dao.UserMapper.findById"))
    })
    List<LineUserList> getMemberInLine(@Param("lid") int lid, @Param("offset") int offset, @Param("limit") int limit);

    @Select("select `lid` from line where `name` = #{name}")
    int getLidByName(@Param("name") String name);

    @Insert("insert into line(`name`) values(#{name})")
    void addLine(@Param("name") String name);

    @Insert("insert into Line_Permission(`lid`, `uid`, `r`, `w`, `a`) values(#{lid}, #{uid}, #{r}, #{w}, #{a})")
    void addMember(
            @Param("lid") int lid, @Param("uid") String uid,
            @Param("r") String r, @Param("w") String w, @Param("a") String a
    );

    @Update("update user set `line` = #{lid} where uid = #{uid}")
    void changeLine(@Param("lid") int lid, @Param("uid") String uid);

    @Update("update Line_Permission set `r` = #{r}, `w` = #{w}, `a` = #{a} where uid = #{uid} and lid = #{lid}")
    void renewPermission(
            @Param("lid") int lid, @Param("uid") String uid,
            @Param("r") String r, @Param("w") String w, @Param("a") String a
    );

    @Update("update `line` set `name` = #{name} where lid = #{lid}")
    void setLine(@Param("lid") int lid, @Param("name") String name);

    @Delete("delete from `line` where lid = #{lid}")
    void deleteLine(@Param("lid") int lid);

    @Delete("delete from `Line_Permission` where lid = #{lid} and uid = #{uid}")
    void deleteMember(@Param("lid") int lid, @Param("uid") String uid);
}
